package com.gem.growingtree.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.gem.growingtree.bodyline.BodyData;
import com.gem.growingtree.bodyline.BodyDataService;

public class BodyActivity extends Activity {
	private List x;
	private List y;
	// 新建数据库
	BodyDataService bodyDataService = new BodyDataService(BodyActivity.this);
	double[] aIntegers;
	double[] bFloats;

	public void getXY() {
		List<BodyData> list = bodyDataService.getData();
		Iterator<BodyData> iterator = list.iterator();
		int i = 0;
		aIntegers = new double[list.size()];
		bFloats = new double[list.size()];
		while (iterator.hasNext()) {
			BodyData bodyData = iterator.next();
			aIntegers[i] = Double.valueOf(bodyData.getX().toString())
					.doubleValue();
			bFloats[i] = Double.valueOf(bodyData.getY().toString())
					.doubleValue();
			i++;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_body);
		String[] titles = new String[] { "太胖", "太瘦", "我的曲线" };
		// 准备所有的数据
		x = new ArrayList();
		y = new ArrayList();

		x.add(new double[] { 1, 3, 5, 7, 9, 11, 13, 15, 17 });
		x.add(new double[] { 1, 3, 5, 7, 9, 11, 13, 15, 17 });

		y.add(new double[] { 18, 18.3, 18.3, 18.3, 18.3, 18.3, 18.3, 18.3, 18.3 });
		y.add(new double[] { 24, 24.1, 24.1, 24.1, 24.1, 24.1, 24.1, 24.1, 24.1 });

		getXY();
		x.add(aIntegers);
		y.add(bFloats);

		XYMultipleSeriesDataset dataset = buildDataset(titles, x, y);

		int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.RED };
		PointStyle[] styles = new PointStyle[] { PointStyle.TRIANGLE,
				PointStyle.SQUARE, PointStyle.POINT };
		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles, true);

		renderer.setChartTitleTextSize(20);
		// 设置X轴的值
		renderer.addTextLabel(0, "0");

		renderer.addTextLabel(1, "2014-1");
		renderer.addTextLabel(2, "2014-2");
		renderer.addTextLabel(3, "2014-3");
		renderer.addTextLabel(4, "2014-4");
		renderer.addTextLabel(5, "2014-5");
		renderer.addTextLabel(6, "2014-6");
		renderer.addTextLabel(7, "2014-7");
		renderer.addTextLabel(8, "2014-8");
		renderer.addTextLabel(9, "2014-9");
		renderer.addTextLabel(10, "2014-10");
		renderer.addTextLabel(11, "2014-11");
		renderer.addTextLabel(12, "2014-12");
		renderer.addTextLabel(13, "2015-1");
		renderer.addTextLabel(14, "2015-2");
		renderer.addTextLabel(15, "2015-3");
		renderer.addTextLabel(16, "2015-4");
		renderer.addTextLabel(17, "2015-5");
		renderer.addTextLabel(18, "2015-6");
		renderer.addTextLabel(19, "2015-7");
		renderer.addTextLabel(20, "2015-8");
		renderer.setAxisTitleTextSize(20);
		renderer.setXLabels(0);
		renderer.setYLabels(20);
		setChartSettings(renderer, "Growing Line", "Time", "BMI", 0, 12, 0, 20,
				Color.WHITE, Color.WHITE);

		renderer.setLegendTextSize(15);

		renderer.setXLabelsAlign(Align.RIGHT);// 刻度线与刻度标注之间的相对位置关系
		renderer.setYLabelsAlign(Align.CENTER);// 刻度线与刻度标注之间的相对位置关系
		renderer.setZoomButtonsVisible(true);// 是否显示放大缩小按钮

		renderer.setPanLimits(new double[] { -10, 20, -10, 40 }); // 设置拖动时X轴Y轴允许的最大值最小值.
		renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });// 设置放大缩小时X轴Y轴允许的最大最小值.
		// 获取展示的窗口
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.chartlayout);
		GraphicalView mChartView = ChartFactory.getLineChartView(this, dataset,
				renderer);
		layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));

	}

	protected XYMultipleSeriesDataset buildDataset(String[] titles,
			List xValues, List yValues) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		int length = titles.length; // 有几条线
		for (int i = 0; i < length; i++) {
			XYSeries series = new XYSeries(titles[i]); // 根据每条线的名称创建
			double[] xV = (double[]) xValues.get(i); // 获取第i条线的数据
			double[] yV = (double[]) yValues.get(i);
			int seriesLength = xV.length; // 有几个点

			for (int k = 0; k < seriesLength; k++) // 每条线里有几个点
			{
				series.add(xV[k], yV[k]);
			}

			dataset.addSeries(series);
		}

		return dataset;
	}

	protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
			PointStyle[] styles, boolean fill) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			r.setFillPoints(fill);
			// 显示值
			r.setDisplayChartValues(true);
			r.setChartValuesTextSize(20);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.body, menu);
		return true;
	}

}
